package net.towee.server.ftp.files;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.List;

import net.towee.model.ProfileArea;
import net.towee.model.discussion.Discussion;
import net.towee.model.discussion.DiscussionSubject;
import net.towee.model.discussion.Message;
import net.towee.model.repo.AttachmentIdentity;
import net.towee.model.repo.DocumentData;
import net.towee.model.repo.DocumentNode;
import net.towee.server.persistence.DiscussionStore;
import net.towee.server.persistence.DocumentStore;
import net.towee.shared.exceptions.PersistenceException;

import org.apache.ftpserver.ftplet.FtpFile;

/**
 * This file represent document data by one file
 * 
 * @author c58
 */
public class DocumentDataFtpFile extends AbstractFtpFile {
	private final DocumentNode node;
	private final DocumentStore docStore;
	private final DiscussionStore discStore;
	private final DocumentData doc;

	public DocumentDataFtpFile(DocumentNode node, DocumentStore docStore, DiscussionStore discStore) {
		super("Document.html", false, 0, node.getDocumentIdentity().getGroup().getAlias(), node
				.getDocumentIdentity().getAuthor().getFullName(), FtpFileType.DESCRIPTION, "/"
				+ node.getDocumentIdentity().getGroup().getAlias() + node.getPath() + "/"
				+ node.getDocumentIdentity().getName() + "/" + "Document.html");

		this.node = node;
		this.docStore = docStore;
		this.discStore = discStore;
		
		// Get document
		try {
			this.doc = docStore.getDocument(node.getDocumentIdentity().getId(), "desc", "profileAreas",
					"discussion", "attach", "updateDate");
		} catch (PersistenceException e) {
			throw new IllegalArgumentException("No document found with ID: " + node.getDocumentIdentity().getId());
		}
	}

	@Override
	public long getLastModified() {
		return doc.getUpdateDate().getTime();
	}

	@Override
	public List<FtpFile> listFiles() {
		return null;
	}

	@Override
	public OutputStream createOutputStream(long offset) throws IOException {
		return null;
	}

	@Override
	public InputStream createInputStream(long offset) throws IOException {
		// Create streams
		StringBuilder out = new StringBuilder();

		// Create document
		writeSimpleDocumentRepresentation(out, doc);

		// Get input stream and skip offset bytest
		InputStream is = new ByteArrayInputStream(out.toString().getBytes("UTF-8"));
		is.skip(offset);

		return is;
	}

	private void writeSimpleDocumentRepresentation(Appendable out, DocumentData doc) throws IOException {
		out.append("<html>");

		// Head
		out.append("<head>");
		out.append("<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\" />");
		out.append("<style type=\"text/css\">h1{padding:0px;margin:0px;}#author{font-size:13px;color:#aaa;margin-bottom:16px;}#messages{margin-left:10px;}</style>");
		out.append("<title>" + node.getDocumentIdentity().getName() + "</title>");
		out.append("</head>");

		// Body
		out.append("<body>");
		out.append("<h1>" + node.getDocumentIdentity().getName() + "</h1>");
		out.append("<div id=\"author\"><i>by <b>" + node.getDocumentIdentity().getAuthor().getFullName()
				+ "</b> at <b>" + new SimpleDateFormat("d MMMM y hh:mm a").format(doc.getUpdateDate())
				+ "</b></i></div>");

		for (ProfileArea area : doc.getProfile().getProfileAreas()) {
			out.append("<b>" + area.getName() + "</b>: " + area.getValue() + "<br />");
		}

		out.append("<p>" + doc.getDescription() + "</p>");
		out.append("<h2>Attachments</h2>");
		out.append("<ul>");

		for (AttachmentIdentity att : doc.getAttachments().getAttachmentList()) {
			out.append("<li><a href=\"attachments/" + att.getName() + "\">" + att.getName()
					+ "</a>, downloaded " + att.getDownloads() + ", size " + att.getSize()
					+ " bytes</li>");
		}

		out.append("</ul>");
		out.append("<h2>Discussion</h2>");
		out.append("<div id=\"messages\">");

		// Get full discussion id needed
		Discussion disc = doc.getDiscussion();
		if (disc.getMessagesList().size() < disc.getCount())
			disc = discStore.getDiscussion(doc.getIdentity().getId(), DiscussionSubject.DOCUMENT);

		for (Message mess : doc.getDiscussion().getMessagesList()) {
			out.append("<div class=\"message\">");
			out.append("<b>" + mess.getAuthor().getFullName() + "</b> - ");
			out.append(mess.getText());
			out.append("  <i>" + new SimpleDateFormat("d MMMM y hh:mm a").format(mess.getCreationDate())
					+ "</i>");
			out.append("</div>");
		}

		out.append("</div>");
		out.append("</body>");

		out.append("</html>");
	}
}
